use std::ops::Deref;

use crate::{CachedID, JClassID, JEnv, JNameMaker, JObject, JPlainMarker};

pub struct JThrowable {
	__supper: JObject,
}
impl Deref for JThrowable {
	type Target = JObject;
	fn deref(&self) -> &Self::Target {
		&self.__supper
	}
}
impl AsRef<JObject> for JThrowable {
	fn as_ref(&self) -> &JObject {
		&self.__supper
	}
}
impl JNameMaker for JThrowable {
	const JAVA_PATH: &'static [u8] = b"java/lang/Throwable\0";
}
impl JPlainMarker for JThrowable {
	fn class(env: Option<&JEnv>) -> Option<JClassID> {
		static CACHE: CachedID = CachedID::new();
		CACHE.get(|| JEnv::env(env)?.find_class(Self::JAVA_PATH))
	}
}
impl JThrowable {
	pub fn from_sys<'a>(obj: sys::jobject) -> Option<&'a Self> {
		if obj.is_null() {
			None
		} else {
			Some(unsafe { &*(obj as *const JThrowable) })
		}
	}
}
